package cn.nubia.admin.sysconfig.controller;

import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.sysconfig.bean.MenuResource;
import cn.nubia.admin.sysconfig.dto.MenuResourceDTO;
import cn.nubia.admin.sysconfig.service.MenuResourceService;
import cn.nubia.admin.sysconfig.bean.Role;
import cn.nubia.admin.sysconfig.bean.RoleResource;
import cn.nubia.admin.sysconfig.query.RoleQueryParam;
import cn.nubia.admin.sysconfig.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Jason on 2015/6/27.
 * 角色关联操作Controller.
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {


    /**
     * 角色管理Service.
     */
    @Autowired
    private RoleService roleService;

    /**
     * 菜单权限Service.
     */
    @Autowired
    private MenuResourceService menuResourceService;


    /**
     * 获取角色列表.
     *
     * @param roleQueryParam 角色查询参数
     * @return 返回带分页的角色列表.
     */
    @RequestMapping(value = "roleList")
    @RequiresPermissions("sys:syscfg:role:list")
    public ModelAndView getRoleList(RoleQueryParam roleQueryParam) {
        ModelAndView modelAndView = new ModelAndView("/role/roleList");
        List<Role> rolePaged = roleService.findList(roleQueryParam);
        modelAndView.addObject("roleList", rolePaged);
        return modelAndView;
    }


    /**
     * 查看角色
     *
     * @param id 角色id
     * @return 角色信息.
     */
//    @RequiresPermissions("role:list")
    @RequestMapping(value = "view", method = RequestMethod.GET)
    public ModelAndView view(Integer id) {
        ModelAndView modelAndView = new ModelAndView("/role/roleForm");
        Role role = roleService.get(id);
        if (role == null) {
            role = new Role();
        }
        modelAndView.addObject("role", role);
        return modelAndView;
    }

//    @RequiresPermissions("role:auth")
    @RequestMapping(value = "viewPerm", method = RequestMethod.GET)
    public ModelAndView viewPerm(Integer id) {
        ModelAndView modelAndView = new ModelAndView("/role/rolePerm");
        Role role = roleService.get(id);

        modelAndView.addObject("role", role);
        return modelAndView;
    }


    /**
     * 修改或者新增角色
     *
     * @param role 角色对象
     * @return 是否成功
     */
    @ResponseBody
//    @RequiresPermissions("role:save")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Response<Boolean> save(Role role) {
        roleService.save(role);
        return Response.getSuccessResponse(true);
    }


    /**
     * 修改或者新增角色
     *
     * @param id 角色id
     * @return 是否成功
     */
    @ResponseBody
//    @RequiresPermissions("role:del")
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public Response<Boolean> delete(Integer id) {
        roleService.delete(id);
        return Response.getSuccessResponse(true);
    }


    /**
     * 返回角色权限树.
     *
     * @param id 角色Id
     * @return 角色权限树.
     */
    @ResponseBody
//    @RequiresPermissions("role:auth")
    @RequestMapping(value = "getResourceTree", method = RequestMethod.GET)
    public Response<List<MenuResourceDTO>> getResourceTree(Integer id) {
        Role role = roleService.get(id);
        List<RoleResource> roleResources = roleService.getRoleResourceList(role);
        List<MenuResource> menuResources = menuResourceService.getMenuResourceList();

        List<MenuResourceDTO> retList = new ArrayList<MenuResourceDTO>();


        for (MenuResource resource : menuResources) {
            MenuResourceDTO menuResourceDTO = new MenuResourceDTO();
            BeanUtils.copyProperties(resource, menuResourceDTO);
            menuResourceDTO.setRoleId(id);
            for (RoleResource roleResource : roleResources) {
                if (roleResource.getResId() == resource.getId()) {
                    menuResourceDTO.setChecked(true);
                }
            }
            retList.add(menuResourceDTO);
        }
        return Response.getSuccessResponse(retList);
    }




    /**
     * 添加角色权限.
     *
     * @param roleResource 角色权限
     * @return
     */
    @ResponseBody
//    @RequiresPermissions("role:auth")
    @RequestMapping(value = "addRoleResource", method = RequestMethod.POST)
    public Response<Boolean> addRoleResource(RoleResource roleResource) {
        roleService.saveRoleResource(roleResource);
        return Response.getSuccessResponse(true);
    }

    @ResponseBody
//    @RequiresPermissions("role:auth")
    @RequestMapping(value = "deleteRoleResource", method = RequestMethod.POST)
    public Response<Boolean> deleteRoleResource(RoleResource roleResource) {
        roleService.deleteRoleResource(roleResource);
        return Response.getSuccessResponse(true);
    }


}
